package ilincar.systemcore.ddpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import org.json.JSONObject;

import ilincar.utils.IlincarAction;

/**
 * Created by ilincar on 2016/12/15.
 * 接收DDPUSH 发送过来的广播消息
 */

public class DDPushClientReceiver extends BroadcastReceiver {

    private final String TAG = "DDPushClientReceiver";

    private Context mContext = null;

    private IntentFilter mIntentFilter = null;

    private OnDDPushListener ddPushListener;

    public DDPushClientReceiver(Context context){
        this.mContext = context;
        mIntentFilter = new IntentFilter();
    }

    public void setDdPushListener(OnDDPushListener ddPushListener) {
        this.ddPushListener = ddPushListener;
    }

    public void registerReceiver(){
        //mIntentFilter.addAction(IlincarAction.ACTION_DDUPUSH_SERVER);     //接收DDPUSH 发送过来的广播
        mIntentFilter.addAction("push_act_from_server");
        mContext.registerReceiver(this, mIntentFilter);
    }


    public void unregisterReceiver(){
        mContext.unregisterReceiver(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if("push_act_from_server".equals(action)){
            String text = intent.getStringExtra("param_msg");
            String code = "";
            JSONObject data = null;
            try {
                String respone = DDPushUtils.getEncodeAndGzip(text);
                JSONObject jsonObject = new JSONObject(respone);
                if(jsonObject.has("order")){
                    JSONObject order = jsonObject.getJSONObject("order");
                    if(order.has("code")){
                        code = order.getString("code");
                    }
                    if(order.has("data")){
                        data = order.getJSONObject("data");
                    }

                }
                else {
                    if(jsonObject.has("code")) {
                        code = jsonObject.getString("code");
                    }

                    if(jsonObject.has("data")){
                        data = jsonObject.getJSONObject("data");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            if(code != null && !code.equals("")){
                if(DDPushUtils.DDPUSH_CAR_POSITION.equals(code)){
                    //请求当前车机位置
                    ddPushListener.onRequestCarPosition();
                }else if(DDPushUtils.DDPUSH_PRENAVIGATION.equals(code)){
                    //请求预导航
                    ddPushListener.onRequestPrenavigation(data);
                }else if(DDPushUtils.DDPUSH_BIND_DATA.equals(code)){
                    //返回车机绑定数据
                    ddPushListener.onReceiveBindData(data);
                }else if(DDPushUtils.DDPUSH_OBD_DATA.equals(code)){
                    //请求OBD数据
                    ddPushListener.onRequestOBDData();
                }else if(DDPushUtils.DDPUSH_TAKE_PHOTO.equals(code)){
                    //请求拍照
                    ddPushListener.onRequestTakePhoto();
                }else if(DDPushUtils.DDPUSH_WEIXIN_ACCESS.equals(code)){
                    //微信接人
                    ddPushListener.onRequestWeiXinAccess(data);
                }else if(DDPushUtils.DDPUSH_REQUEST_VIDEO.equals(code)){
                    //请求上传视频
                    ddPushListener.onRequestUploadVideo(data);
                }else if(DDPushUtils.DDPUSH_LIVE_CAMERA_VIDEO.equals(code)){
                    //请求时时视频流
                    ddPushListener.onRequestLiveCamera(data);
                }else if(DDPushUtils.DDPUSH_LIVE_CAMERA_STOP_VIDEO.equals(code)){
                    //请求停止时时视频流
                    ddPushListener.onRequestStopLiveCamera();
                }
            }
        }
    }
}
